//
//  QuDanHomeHeaderView.m
//  QuDanOEM
//
//  Created by mac on 2021/10/23.
//

#import "QuDanHomeHeaderView.h"
#import "SDCycleScrollView.h"
#import "QuDanHomeCategoryViewCell.h"
#import "QuDanCategoryModel.h"
#import "QuDanBannerModel.h"
#import "QuDanHomeCategoryController.h"
@interface QuDanHomeHeaderView () <SDCycleScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *categoryCollectionview;
@property (nonatomic,strong)NSMutableArray *categoryListArr;
@property (nonatomic,strong)NSMutableArray *bannerListArr;
@property (strong, nonatomic) SDCycleScrollView *adScrollView;
@end
@implementation QuDanHomeHeaderView
- (NSMutableArray *)bannerListArr
{
    if (!_bannerListArr) {
        _bannerListArr = [NSMutableArray array];
    }
    return _bannerListArr;
}
- (NSMutableArray *)categoryListArr
{
    if (!_categoryListArr) {
        _categoryListArr = [NSMutableArray array];
    }
    return _categoryListArr;
}
- (SDCycleScrollView *)adScrollView
{
    if (!_adScrollView) {
        _adScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@"material_default_bg"]];
        _adScrollView.backgroundColor = QD_RGB(38, 38, 38);
        _adScrollView.currentPageDotColor = QD_RGB(250, 204, 72);
        _adScrollView.pageDotColor = QD_RGB(208, 208, 208);
        _adScrollView.showPageControl = YES;
        _adScrollView.autoScrollTimeInterval = 3;
        _adScrollView.contentMode = UIViewContentModeScaleAspectFill;
        _adScrollView.layer.cornerRadius = 8.0f;
        _adScrollView.layer.masksToBounds = YES;
        _adScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    }
    return _adScrollView;
}
- (UICollectionView *)categoryCollectionview
{
    if (!_categoryCollectionview) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flow.minimumLineSpacing = 8;
        flow.minimumInteritemSpacing = 8;
        _categoryCollectionview = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
        _categoryCollectionview.backgroundColor = [UIColor clearColor];
        _categoryCollectionview.showsVerticalScrollIndicator = NO;
        _categoryCollectionview.showsHorizontalScrollIndicator = NO;
        _categoryCollectionview.delegate = self;
        _categoryCollectionview.dataSource = self;
        [_categoryCollectionview registerNib:[UINib nibWithNibName:@"QuDanHomeCategoryViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanHomeCategoryViewCell"];
    }
    return _categoryCollectionview;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
        [self seutpData];
    }
    return self;
}
-(void)seutpData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    NSString *baseUrl = [NSString stringWithFormat:@"%@%@",QD_BaseURL,@"/qw/app/index"];
    [[QuDanNetworkManger sharedNetworkManger]GET:baseUrl parameters:param success:^(QuDanResponseModel * _Nonnull response) {
        NSLog(@"index:data:%@",response.data);
        if (response.code == 200) {
            [self.bannerListArr removeAllObjects];
            [self.categoryListArr removeLastObject];
            NSMutableArray *imageArr = [NSMutableArray array];
            self.bannerListArr = [QuDanBannerModel mj_objectArrayWithKeyValuesArray:response.data[@"banner"]];
            self.categoryListArr = [QuDanCategoryModel mj_objectArrayWithKeyValuesArray:response.data[@"category"]];
            for (QuDanBannerModel *model in self.bannerListArr) {
                [imageArr addObject:model.imageUrl];
            }
            self.adScrollView.imageURLStringsGroup = imageArr;
            [self.categoryCollectionview reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:response.msg];
            return;
        }
    } failure:^(NSError * _Nonnull error) {
            
    }];
}
-(void)setupUI
{
    [self addSubview:self.categoryCollectionview];
    [self.categoryCollectionview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(80);
    }];
    [self addSubview:self.adScrollView];
    [self.adScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.top.mas_equalTo(self.categoryCollectionview.mas_bottom).offset(10);
        make.bottom.offset(-10);
    }];
    self.adScrollView.imageURLStringsGroup = self.bannerListArr;
}
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    if([self.delegate respondsToSelector:@selector(clickSelectScrollViewIndex:)]){
        [self.delegate clickSelectScrollViewIndex:index];
    }
}
/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index
{
    
}
#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.categoryListArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    QuDanHomeCategoryViewCell *categorycell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanHomeCategoryViewCell" forIndexPath:indexPath];
    QuDanCategoryModel *model = self.categoryListArr[indexPath.row];
    categorycell.titleLab.text = model.caption;
    [categorycell.typeImage sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"qd_test"]];
    return categorycell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(8, 8, 8, 8);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(QD_IPHONE_WIDTH/5, 80);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    QuDanHomeCategoryController *categoryvc = [[QuDanHomeCategoryController alloc]init];
    categoryvc.hidesBottomBarWhenPushed = YES;
    QuDanCategoryModel *model = self.categoryListArr[indexPath.row];
    categoryvc.model = model;
    [self.viewController.navigationController pushViewController:categoryvc animated:YES];
}
@end

